<?php

class Core_Db_Table_Rowset_Abstract extends Zend_Db_Table_Rowset_Abstract
{
	function getRandom()
	{
		if($this->count() > 2)
		{
			return $this->getRow(rand(0,$this->count()-1));
		}
		elseif($this->count() == 1)
		{
			return $this->getRow(0);
		}
		return null;
	}
	function getRows($key = null, $val = null)
	{
		$entries = array();
		for($i=0;$i < $this->count();$i++)
		{
			if($key === null)
			{
				$entries[] = $this->getRow($i);
			}
			elseif($val === null)
			{
				$_key = (substr($key, -2) == '()')
								  ?call_user_func(array($this->getRow($i), substr($key, 0, -2)))
									:$this->getRow($i)->{$key};
										 
				$entries[$_key] = $this->getRow($i);
			}
			else
			{
				$_key = (substr($key, -2) == '()')
									?call_user_func(array($this->getRow($i), substr($key, 0, -2)))
									:$this->getRow($i)->{$key};
	
				$_val = (substr($val, -2) == '()')
									?call_user_func(array($this->getRow($i), substr($val, 0, -2)))
									:$this->getRow($i)->{$val};
	
				$entries[$_key] = $_val;
			}
		}
		return $entries;
	}
	function update($key, $value)
	{
		$result 	= 0;
		$changes	= is_array($key)?$key:array($key => $value);
		foreach($this->_data as $i => $data)
		{
			$entry 	 = $this->getRow($i);
			$result += $entry->setData($changes)->save();
		}
		return $result;
	}
	function delete()
	{
		$result = 0;
		foreach($this->_data as $i => $data)
		{
			$entry 	 = $this->getRow($i);
			$result += $entry->delete();
		}
		return $result;
	}
}